package com.bishe.demo.modules.other.service.impl;

import com.bishe.demo.modules.common.entity.ResultEntity;
import com.bishe.demo.modules.common.entity.SearchBean;
import com.bishe.demo.modules.other.dao.MessageDao;
import com.bishe.demo.modules.other.dao.SceneryMessageDao;
import com.bishe.demo.modules.other.dao.UserMessageDao;
import com.bishe.demo.modules.other.entity.Message;
import com.bishe.demo.modules.other.entity.SceneryMessage;
import com.bishe.demo.modules.other.entity.UserMessage;
import com.bishe.demo.modules.other.service.MessageService;
import com.github.pagehelper.PageHelper;
import com.github.pagehelper.PageInfo;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;

import javax.transaction.Transactional;
import java.time.LocalDateTime;
import java.util.Collections;
import java.util.List;
import java.util.Optional;

@Service
public class MessageServiceImpl implements MessageService {
    @Autowired
    private MessageDao messageDao;

    @Autowired
    private UserMessageDao userMessageDao;

    @Autowired
    private SceneryMessageDao sceneryMessageDao;


    @Override
    @Transactional
    public ResultEntity<Message> insertMessage(Message message) {
        message.setCreateTime(LocalDateTime.now());
        messageDao.insertMessage(message);
        sceneryMessageDao.insertSceneryMessage(new SceneryMessage(message.getSceneryId(),message.getMessageId()));
        userMessageDao.insertUserMessage(new UserMessage(message.getUser().getUserId(),message.getMessageId()));
        return new ResultEntity<Message>(ResultEntity.ResultStatus.SUCCESS.code, "添加成功", message);
    }

    @Override
    @Transactional
    public ResultEntity<Message> updateMessage(Message message) {
        messageDao.updateMessage(message);
        return new ResultEntity<Message>(ResultEntity.ResultStatus.SUCCESS.code, "修改成功", message);
    }

    @Override
    @Transactional
    public ResultEntity<Message> deleteMessageByMessageId(int messageId) {
        messageDao.deleteMessageByMessageId(messageId);
        userMessageDao.deleteUserMessageByMessageId(messageId);
        sceneryMessageDao.deleteSceneryMessageByMessageId(messageId);
        return new ResultEntity<Message>(ResultEntity.ResultStatus.SUCCESS.code, "删除成功");
    }

    @Override
    public Message getMessageByMessageId(int messageId) {
        return messageDao.getMessageByMessageId(messageId);
    }

    @Override
    public List<Message> getMessagesByUserId(int userId) {
        return messageDao.getMessagesByUserId(userId);
    }

    @Override
    public List<Message> getMessagesBySceneryId(int sceneryId) {
        return messageDao.getMessagesBySceneryId(sceneryId);
    }

    @Override
    public PageInfo<Message> getMessagesBySearchBean(SearchBean searchBean) {
        searchBean.initSearchBean();
        PageHelper.startPage(searchBean.getCurrentPage(), searchBean.getPageSize());
        return new PageInfo<Message>(Optional
                .ofNullable(messageDao.getMessagesBySearchBean(searchBean))
                .orElse(Collections.emptyList()));
    }
}
